package com.hzw.net.weiget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.graphics.drawable.BitmapDrawable
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.util.AttributeSet
import android.view.View
import com.hzw.core.handler.mainHandler
import com.hzw.core.resources.dp2px
import com.hzw.core.resources.drawable
import com.hzw.net.R
import java.lang.ref.WeakReference

/**
 *
 * @Author:         HuaZhongWei
 * @CreateDate:     2021/1/15 13:44
 *
 * @Description:
 *
 */
class LoadingView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private val rangeColor = Color.parseColor("#e7e7e7")
    private val rangWidth = 3f.dp2px()

    private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        style = Paint.Style.STROKE
        strokeWidth = rangWidth
        strokeCap = Paint.Cap.ROUND
    }

    private val rotateRangeColor = Color.parseColor("#449867")

    //转动部分幅度
    private val sweepAngle = 50f

    private var angle = 0f

    private var centerX = 0
    private var centerY = 0

    private var insideRangeRadius = 0
    private var outsideRangeRadius = 0

    private var rangePadding = 12.dp2px()
    private val outsideRangeRect = RectF()

    private val icon = (drawable(R.drawable.ic_qh)!! as BitmapDrawable).bitmap
    private val iconDstRect = RectF()

    init {
        setWillNotDraw(false)
    }

    private var isRotate = false

    fun isRotate() = isRotate

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
        centerX = measuredWidth / 2
        centerY = measuredHeight / 2

        outsideRangeRadius = ((measuredHeight - rangePadding * 2) / 2f).toInt()
        insideRangeRadius = (outsideRangeRadius - rangWidth).toInt()

        outsideRangeRect.set(
            centerX - insideRangeRadius - rangWidth / 2,
            centerY - insideRangeRadius - rangWidth / 2,
            centerX + insideRangeRadius + rangWidth / 2,
            centerY + insideRangeRadius + rangWidth / 2
        )

        iconDstRect.set(
            centerX - icon.width / 2f,
            centerY - icon.height / 2f,
            centerX + icon.width / 2f,
            centerY + icon.height / 2f
        )
    }

    private var rotateAngle = 0f

    private fun refreshAngle() {
        if (isRotate) {
            rotateAngle += 5f
            angle = rotateAngle % 360
            invalidate()
        }
    }

    fun start() {
        if (!isRotate) {
            isRotate = true
            loadingHandler.sendEmptyMessage(REFRESH_LOADING)
        }

    }

    fun stop() {
        if (isRotate) {
            isRotate = false
            loadingHandler.removeMessages(REFRESH_LOADING)
        }
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        drawFixedRange(canvas)
        drawMoveRange(canvas)
        drawIcon(canvas)
    }

    private fun drawIcon(canvas: Canvas) {
        canvas.drawBitmap(icon, null, iconDstRect, null)
    }

    private fun drawMoveRange(canvas: Canvas) {
        paint.strokeWidth = rangWidth
        paint.color = rotateRangeColor
        canvas.drawArc(outsideRangeRect, angle, sweepAngle, false, paint)
    }

    /**
     * 绘制固定的圆环
     */
    private fun drawFixedRange(canvas: Canvas) {
        paint.strokeWidth = rangWidth
        paint.color = rangeColor
        canvas.drawArc(outsideRangeRect, 0f, 360f, false, paint)
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        stop()
        loadingHandler.removeCallbacksAndMessages(null)
    }

    companion object {
        private const val REFRESH_LOADING = 0X11111
        private const val REFRESH_TIME = 15L
    }

    private val loadingHandler = LoadingHandler(this)

    class LoadingHandler(loadingView: LoadingView) : Handler(Looper.getMainLooper()) {
        private val reference = WeakReference(loadingView)
        override fun handleMessage(msg: Message) {
            if (msg.what == REFRESH_LOADING) {
                reference.get()?.refreshAngle()
                sendEmptyMessageDelayed(REFRESH_LOADING, REFRESH_TIME)
            }
        }
    }
}